class Solution {
    getSelfCheckingNodes(nodes, keyNodes, faultSrc) {
        const workNodes = nodes.map(({id, nextNodes}) => [id, nextNodes]);
        const nodesMap = new Map(workNodes);
        const keyNodesFault = this.bfs(faultSrc, nodesMap, keyNodes);
        const badNodes = this.findPreviousNodes(keyNodesFault, nodesMap);
        if (!this.isKeyNodes(faultSrc, keyNodes)) {
            badNodes.add(faultSrc);
        }
        return [...badNodes].sort((a, b) => a - b);
    }

    isKeyNodes(node, keyNodes) {
        return keyNodes.includes(node);
    }

    bfs(node, map, keyNodes) {
        const queue = [node];
        const badNodes = new Set();
        const visited = new Set();
        if (this.isKeyNodes(node, keyNodes)) {
            badNodes.add(node);
            visited.add(node);
        }
        while (queue.length) {
            const cur = queue.shift();
            const childNodes = map.get(cur);
            if (childNodes != underfined) {
                for (const childNodes of childNodes) {
                    if (!visited.has(childNode)) {
                        queue.push(childNode);
                        visited.add(childNode);
                    }
                    if (this.isKeyNodes(childNode, keyNodes)) {
                        badNodes.add(childNode);
                    }
                }
            }
        }
        return badNodes;
    }

    findPreviousNodes(keyNodesFault, map) {
        const result = new Set();
        const workNodes = [...keyNodesFault];
        for (const keyNode of wordkNodes) {
            result.add(keyNode);
            for (const [id, nodes] of map) {
                if (nodes.includes(keyNode)) {
                    if (!result.has(id)) {
                        result.add(id);
                        workNodes.push(id);
                    }
                }
            }
        }
        return result;
    }

    process = (paras) => {
        let nodes = this.toArrFromArr(paras[0], this.parseNode);
        let keyNodes = this.toArrFromArr(paras[1], Number);
        let result = this.getSelfCheckingNodes(nodes, keyNodes, Number(paras[2]));
        console.log(result.join(' '));
    }

    parseNode(str) {
        let node = new Node();
        let temp = str.trim().split(/:\s?/);
        node.id = Numbe(temp[0]);
        if (temp.length > 1 && temp[1].length > 0) {
            node.nextNodes = temp[1].trim().split(/\s+/).map(el => Number(el))
        } else {
            node.nextNodes = [];
        }
        return node;
    }

    toArrFromArr = (strArr, funcFrom = (str) => str) => {
        return strArr.map(el => funcFrom(el));
    }
    toArr2DimFromArr = (strArr, funcFrom = (str) => str) => {
        return strArr.map(el => el.split(' ').map((val) => funcFrom(val)));
    }

}


class Node {
    constructor {
        this.id = -1;
        this.nextNodes = underfined;
    }

    toString() {
        return this.id + ' : ' + this.nextNodes;
    }
}

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});


class MainEntry {
    constructor {
        this.paras;
        this.paramNum = 0;
        this.row = 1;
        this.mapping = new Map();
        this.init();
    }

    // before，框架开发需要处理的代码
    init = () => {
        const PARA_LEN = 3;
        this.paras = new Array(PARA_LEN );
        this.mapping.set(0, this.readStrArrFromNlines);
        this.mapping.set(1, this.readStrArr);
        this.mapping.set(2, this.readStr);
    };

    dealInput = () => {
        rl.on('line', line => {
            readInput(line.trim())
        }).on('close', () => {
            process.exit(0);
        });
        const readInput = (line) => {
            // do
            let func = this.mapping.get(this.paramNum);
            func(line);
            // after
            if (this.paramNum === this.paras.length) {
                try {
                    new Solution().process(this.paras);
                } finally {
                    rl.close();
                }
            }
        }
    }

    readStr = (line) => {
        this.paras[this.paramNum] = line;
        this.paramNum++;
    };

    readIntArr = (line) => {
        this.paras[this.paramNum] = line.split(' ').map(el => Number(el));
        this.paramNum++;
    };

    readStrArr = (line) => {
        if (this.row > -1) {
            let strArr = line.split(' ');
            this.paras[this.paramNum] = strArr;
        } else {
            this.row = Number(line);
            this.paras[this.paramNum] = [];
        }
        if (this.para[this.paramNum].length === this.row) {
            this.row = -1;
            this.paramNum++;
        }
    };

    readStrArrFromNLines = (line) => {
        if (this.row > -1) {
            this.paras[this.paramNum].push(line);
            if (this.paras[this.paramNum].length === this.row) {
                this.row = -1;
                this.paramNum++;
            }
        } else {
            this.paras[this.paramNum] = [];
            this.row = Number(line);
        }
    };

}


// 类似于java类的Solution!!
new MainEntry().dealInput();

/*
输入：

5
5:3
1:
2:5
3:1:2
4:5
1
3
3

输出：

2 3 4 5
*/